package com.zhipu.balance.ui.start


import android.annotation.SuppressLint
import android.os.Build
import android.os.Bundle
import android.text.TextUtils
import com.blankj.utilcode.util.*
import com.hjq.permissions.OnPermissionCallback
import com.hjq.permissions.Permission
import com.hjq.permissions.XXPermissions
import com.zhipu.balance.R
import com.zhipu.balance.common.Constants
import com.zhipu.balance.databinding.ActivityStartBinding
import com.zhipu.balance.mobile.temporaryClass.Activate
import com.zhipu.balance.ui.BaseActivity
import com.zhipu.balance.ui.activate.ActivateActivity
import com.zhipu.balance.ui.home.HomeActivity
import com.zhipu.balance.utils.InitializeUtils
import com.zhipu.balance.utils.PermissionsUilst
import com.zhipu.balance.utils.data.DataGenerateUtils
import java.io.File
import java.util.*


/**
 * 启动页
 */
class StartActivity : BaseActivity<ActivityStartBinding>() {
    /**
     * NS
     */
    private var serialNumber: String? = null

    override fun initCreate() {
        super.initCreate()
        setTheme(R.style.NoStatusTheme1)
    }

    override fun initContentView(savedInstanceState: Bundle?): Int {
        return R.layout.activity_start
    }


    @SuppressLint("HardwareIds")
    override fun initView() {
        super.initView()

        if(PermissionsUilst.setAuthority(mContext)){
            /**
             * 跳转页面
             */
            skip()
        }else{
            XXPermissions.with(mContext).permission(Permission.MANAGE_EXTERNAL_STORAGE).request(object : OnPermissionCallback{
                override fun onGranted(permissions: MutableList<String>?, all: Boolean) {
                    if (all) {
                        /**
                         * 跳转页面
                         */
                        skip()
                    }else {
                        InitializeUtils.getDefaultMaker()?.show("部分权限未正常授予");
                    }
                }

                override fun onDenied(permissions: MutableList<String>?, never: Boolean) {
                    if (never) {
                        InitializeUtils.getDefaultMaker()?.show("被永久拒绝授权，请手动授予存储权限");
                        // 如果是被永久拒绝就跳转到应用权限系统设置页面
                        XXPermissions.startPermissionActivity(mContext, permissions);
                    } else {
                        InitializeUtils.getDefaultMaker()?.show("获取存储权限失败");
                    }
                }
            })
        }





    }

    /**
     * 跳转页面
     */
    fun skip(){
        /**
         * 人员数据生成
         */
        DataGenerateUtils.dataPersonnel()
        /**
         * 编号数据生成
         */
        DataGenerateUtils.dataNumber()
        /**
         *
         */
        serialNumber = Build.SERIAL
        /**
         * 判断是不是目录
         */
        if (FileUtils.createOrExistsFile(Constants.ActivatePath + File.separator + "Activate.dat")) {
            val activateContent: String = FileIOUtils.readFile2String(Constants.ActivatePath + File.separator + "Activate.dat")
            when(TextUtils.isEmpty(activateContent)){
                true ->{
                    ActivityUtils.startActivity(ActivateActivity::class.java)
                }
                false ->{
                    val activate: Activate = GsonUtils.fromJson(activateContent, Activate::class.java)
                    if (null != activate && activate.mac.equals(DeviceUtils.getMacAddress().uppercase(Locale.getDefault())) && activate.androidId.equals(serialNumber)) {
                        ActivityUtils.startActivity(HomeActivity::class.java)
                    }else{
                        ActivityUtils.startActivity(ActivateActivity::class.java)
                    }
                }
            }
        } else {
            ActivityUtils.startActivity(ActivateActivity::class.java)
        }
        ActivityUtils.finishActivity(StartActivity::class.java)
    }


}